/*
 * SpotBugs - Find Bugs in Java programs
 * Copyright (C) 2025, Guillaume Toison
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package edu.umd.cs.findbugs.ba;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import edu.umd.cs.findbugs.SourceLineAnnotation;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.classfile.MethodDescriptor;

/**
 * A database of calls to "access" methods, i.e. the methods generated by the compiler when an inner class calls a method on its outer class
 *
 * Somewhat similar to {@link InnerClassAccessMap}
 */
public class AccessMethodDatabase {
    private Map<MethodDescriptor, List<AccessMethodLocation>> map = new HashMap<>();

    /**
     * @param accessMethod The method descriptor for the access method
     * @param callerMethod The method descriptor for the inner class' "real" method
     * @param sourceLineAnnotation The annotation for the location where the inner class is calling the access method
     */
    public void addAccessMethod(MethodDescriptor accessMethod, MethodDescriptor callerMethod,
            SourceLineAnnotation sourceLineAnnotation) {
        List<AccessMethodLocation> locations = map.computeIfAbsent(accessMethod, k -> new ArrayList<>());

        locations.add(new AccessMethodLocation(callerMethod, sourceLineAnnotation));
    }

    /**
     * @param accessMethod The method descriptor for the access method
     * @return The locations of the access method caller(s) i.e. the "real" methods in the inner class
     */
    @NonNull
    public List<AccessMethodLocation> getAccessMethodLocations(MethodDescriptor accessMethod) {
        return map.getOrDefault(accessMethod, Collections.emptyList());
    }

    public static class AccessMethodLocation {
        private MethodDescriptor callerMethod;
        private SourceLineAnnotation sourceLineAnnotation;

        public AccessMethodLocation(MethodDescriptor callerMethod, SourceLineAnnotation sourceLineAnnotation) {
            this.callerMethod = callerMethod;
            this.sourceLineAnnotation = sourceLineAnnotation;
        }

        public MethodDescriptor getCallerMethod() {
            return callerMethod;
        }

        public SourceLineAnnotation getSourceLineAnnotation() {
            return sourceLineAnnotation;
        }
    }
}
